package com.example.administrator.shzs.ui;


import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import com.alibaba.fastjson.JSON;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.adapter.IntegraDetialAdapter;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.entity.IntegraDetailData.IntegraDataList;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 当前积分
 */
public class IntegralActivity extends BaseActivity {


    @Bind(R.id.mXRecyclerView_integra_activity)
    XRecyclerView mXRecyclerView;

    private List<IntegraDataList> integraDataLists;
    private List<IntegraDataList> mMoreDatas;

    private IntegraDetialAdapter adapter;

    private String userId;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);

        setViews();

        userId = MyApplication.app.getUserId();
        getIntegraDetailFormServer(1);

        setRefresh();
    }

    private void setRefresh() {

        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        getIntegraDetailFormServer(1);
                        mXRecyclerView.refreshComplete();
                    }
                },500);
            }

            @Override
            public void onLoadMore() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getIntegraDetailFormServer(page);
                        mXRecyclerView.loadMoreComplete();
                    }
                },500);

            }
        });
    }

    /**
     * 从服务器加载数据
     *
     * @param page
     */
    private void getIntegraDetailFormServer( final int page) {

        String url = Contents.USER_INTEGRA_LISTING + userId + "&pagesize=30&pageindex=" + page;

        RequestParams params = new RequestParams(url);

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {

                    JSONObject object = new JSONObject(result);
                    JSONArray array = object.getJSONArray("datalist");

                    if(page==1) {
                        integraDataLists = JSON.parseArray(array.toString(), IntegraDataList.class);
                        setAdapter();
                    }else {
                        mMoreDatas=JSON.parseArray(array.toString(), IntegraDataList.class);
                        if(mMoreDatas.size()==0){
                            return;
                        }
                        integraDataLists.addAll(mMoreDatas);
                        mMoreDatas.clear();
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    /**
     * 适配器
     */
    private void setAdapter() {

        adapter = new IntegraDetialAdapter(this, integraDataLists);
        mXRecyclerView.setAdapter(adapter);
    }

    /**
     * 初始化
     */
    private void setViews() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXRecyclerView.setLayoutManager(manager);
        mXRecyclerView.setRefreshing(true);

        mXRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mXRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin);
        //刷新箭头
        mXRecyclerView.setArrowImageView(R.mipmap.ic_pulltorefresh_arrow);
    }
}
